package com.yyk.yykywd.aa;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;

import java.util.ArrayList;
import java.util.List;

/**
 * 圆形表格图
 */
public class PieChartManager {
    private Context context;
    private PieChart mPieChart;

    public PieChartManager(Context context, PieChart mPieChart) {
        this.context = context;
        this.mPieChart = mPieChart;
        initPieChart();
    }

    private void initPieChart() {

        mPieChart.setUsePercentValues(true);
        //设置隐藏饼图上文字，只显示百分比
        mPieChart.setDrawSliceText(false);
        //显示描述关闭
        mPieChart.getDescription().setEnabled(false);
        mPieChart.setExtraOffsets(5, 10, 5, 5);
        mPieChart.setDragDecelerationFrictionCoef(0.95f);


        /**
         * 中心部分
         */
        //设置为TRUE时，饼中心透明
        mPieChart.setDrawHoleEnabled(true);
        //中间颜色白色
        mPieChart.setHoleColor(Color.WHITE);
        //设置中间文字是否显示。(下图红色框中间的文字部分)
        mPieChart.setDrawCenterText(true);


        //中心半径
        mPieChart.setHoleRadius(0);
        mPieChart.setTransparentCircleColor(Color.WHITE);
        mPieChart.setHoleRadius(70f);
        mPieChart.setTransparentCircleRadius(61f);
        mPieChart.setRotationAngle(0);

        // 触摸旋转
        mPieChart.setRotationEnabled(false);
        mPieChart.setHighlightPerTapEnabled(false);
        Legend legend = mPieChart.getLegend();
        legend.setEnabled(false);
    }

    //设置数据
    public void setData(List<PieChartBean> pieList, List<Integer> colors,String string) {
        List<PieEntry> entries = new ArrayList<>();
        for (int i = 0; i < pieList.size(); i++) {
            PieChartBean pieBean = pieList.get(i);
            entries.add(new PieEntry(Float.valueOf(pieBean.getUnit()), pieBean.getName()));
        }
        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setSelectionShift(5f);

        dataSet.setColors(colors);
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.TRANSPARENT);
        mPieChart.setData(data);
        mPieChart.highlightValues(null);
        //设置中间文字
        mPieChart.setCenterText(generateCenterSpannableText(string));
        mPieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);// 设置pieChart图表展示动画效果

        //刷新
        mPieChart.invalidate();
    }

    //设置中间文字
    private SpannableString generateCenterSpannableText(String string) {
        //原文：MPAndroidChart\ndeveloped by Philipp Jahoda
        SpannableString s = new SpannableString(string);
        return s;
    }

}
